using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class GroundTileMap : MonoBehaviour
{
    [Header("地图配置")]
    public Tilemap ground_tilemap;//拖动获取地面瓦片地图
    public TileBase groundTile;//拖动获取地面规则瓦片Rule Tile
    public Vector2Int lowerLeftCoo = new Vector2Int(-18, -7);//地图起始左下角位置
    public int width = 20;//地图宽度
    public int length = 100;//地图长度
    public float groundStartPro = 0.10f;//生成地面起始点的概率
    public Vector2Int groundLenRan = new Vector2Int(3, 10);//起始地面点生成的长度范围

    [Header("环境配置")]
    public float environmentRich = 0.5f;//环境丰富度
    public Tilemap environ_tilemap;
    public TileBase environTile;//拖动获取环境规则瓦片

    [Header("预制体配置")]
    private List<Vector2> canPrefabVectorList = new List<Vector2>();//能放置预制体的坐标
    public GameObject playerPrefab;//玩家
    public List<GameObject> enemyPrefabList;//敌人
    public List<GameObject> itemsPrefabList;//物品
    void Start()
    {
        CreateMap();

    }

    public void CreateMap()
    {
        GroundStartPoi();
    }

    void GroundStartPoi()
    {
        ground_tilemap.ClearAllTiles();// 清空地面瓦片地图
        for (int i = lowerLeftCoo.x; i < (this.length + lowerLeftCoo.x); i++)
        {
            for (int j = lowerLeftCoo.y; j < (this.width + lowerLeftCoo.y); j++)
            {

                bool IsGround = j < (this.width + 3) ? (Random.value <= groundStartPro) : (Random.value <= groundStartPro + 0.05);//三元表达式，地面三行更容易生成地面起始点
                // bool IsGround = Random.value <= groundStartPro; 
                if (IsGround) GroundExtension(i, j);
            }
        }
        ClearChannel();//生成地面，开始清除产生能走的通道
    }
    void GroundExtension(int i, int j)//从地面起始点开始延长
    {
        int groundLen = Random.Range(groundLenRan.x, groundLenRan.y);
        for (int m = i; m <= i + groundLen; m++)
        {
            //限制超出范围不延长
            int x = Mathf.Clamp(m, lowerLeftCoo.x, this.length + lowerLeftCoo.x - 1);
            ground_tilemap.SetTile(new Vector3Int(x, j, 0), groundTile);
        }
    }

    //清除，产生通道，思路就是从底层有方块的地方，开始判断上面。如果没有方块的话，就清除俩层/三层的通道
    void ClearChannel(){
        for (int i = lowerLeftCoo.x; i < (this.length + lowerLeftCoo.x); i++)
        {
            for (int j = lowerLeftCoo.y; j < (this.width + lowerLeftCoo.y-1); j++)//最高层上面必然没有方块，不需要判断，-1
            {
                if (ground_tilemap.GetTile(new Vector3Int(i, j, 0)) != null)//如果此处不为空方块
                {
                    if (ground_tilemap.GetTile(new Vector3Int(i, j + 1, 0)) == null)//如果此处上方为空方块
                    {
                        ground_tilemap.SetTilesBlock(new BoundsInt(i-2, j + 1, 0,3,3,1),new TileBase[] { null,null, null, null, null, null,null, null, null });//将上方3x3格子置空

                    }
                }
            }
        }
        GenerateEnviron();
    }


    //开始生成花草
    void GenerateEnviron()
    {
        environ_tilemap.ClearAllTiles();// 清空瓦片地图
        for (int i = lowerLeftCoo.x; i < (this.length + lowerLeftCoo.x); i++)
        {
            for (int j = lowerLeftCoo.y; j < (this.width + lowerLeftCoo.y ); j++)
            {
                if (ground_tilemap.GetTile(new Vector3Int(i, j, 0)) ==groundTile&& ground_tilemap.GetTile(new Vector3Int(i, j+1, 0)) == null)//如果此处为地面，上面为空方块
                {
                    //保存能放置预制体的坐标
                    canPrefabVectorList.Add(ground_tilemap.CellToWorld(new Vector3Int(i, j + 1, 0)) + new Vector3(0.5f, 0, 0));

                    if (Random.value < environmentRich)//随机
                    { environ_tilemap.SetTile(new Vector3Int(i, j + 1, 0), environTile);  }
                }



            }
        }
        StartCoroutine(GenerateRandomObjects());
    }


    IEnumerator GenerateRandomObjects()
    {
        GenerateRandomPlayer();//创建主角
        GenerateRandomEnemies();//创建敌人
        GenerateRandomItems();//创建预制体
        yield return null;
    }

    void GenerateRandomPlayer()
    {
        if(playerPrefab == null) return;
        int index = Random.Range(0, canPrefabVectorList.Count);
        Instantiate(playerPrefab, canPrefabVectorList[index], Quaternion.identity);
        canPrefabVectorList.RemoveAt(index);
    }

    void GenerateRandomEnemies()
    {
        if(enemyPrefabList.Count == 0) return;
        for (var i = 0; i < enemyPrefabList.Count; i++)
        {
            int index = Random.Range(0, canPrefabVectorList.Count);
            Instantiate(enemyPrefabList[i], canPrefabVectorList[index], Quaternion.identity);
            canPrefabVectorList.RemoveAt(index);
        }
    }

    void GenerateRandomItems()
    {
        if(itemsPrefabList.Count == 0) return;
        for (var i = 0; i < itemsPrefabList.Count; i++)
        {
            int index = Random.Range(0, canPrefabVectorList.Count);
            Instantiate(itemsPrefabList[i], canPrefabVectorList[index], Quaternion.identity);
            canPrefabVectorList.RemoveAt(index);
        }
    }
}